import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:smart_devices/bruno/src/components/actionsheet/brn_common_action_sheet.dart';
import 'package:smart_devices/bruno/src/components/picker/brn_multi_picker.dart';
import 'package:smart_devices/bruno/src/theme/base/brn_text_style.dart';
import 'package:smart_devices/bruno/src/theme/configs/brn_action_sheet_config.dart';
import 'package:smart_devices/icons/src/antd_icons.dart';
import 'package:smart_devices/params/device.dart';
import 'package:smart_devices/ui/draw_under_line.dart';
import 'package:smart_devices/utils/colors.dart';
import 'package:smart_devices/utils/styles.dart';
import 'package:smart_devices/utils/tools.dart';

import 'screen_logic.dart';

class DeviceScreenView extends GetView<DeviceScreenLogic> {
  const DeviceScreenView({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      builder: (_) {
        return Scaffold(
          backgroundColor:
              ColorsUtils.parseColorFromHexString(ColorsUtils.colorFfff),
          appBar: AppBar(
            leading: IconButton(
                icon: const Icon(
                  Icons.arrow_back_ios,
                  color: Colors.white,
                  size: 15,
                ),
                onPressed: Get.back),
            automaticallyImplyLeading: false,
            backgroundColor:
                ColorsUtils.parseColorFromHexString(ColorsUtils.color0052),
            title: Text(
              '筛选',
              style: StylesUtils.customTextStyle(
                  fontSize: 18, fontWeight: FontWeight.w700),
            ),
            centerTitle: true,
            systemOverlayStyle: SystemUiOverlayStyle.dark,
          ),
          body: SingleChildScrollView(
            padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 20),
            child: Column(
              children: [
                SizedBox(
                  height: 50,
                  child: DrawUnderLine(
                      child: Row(
                    children: [
                      Expanded(
                          child: Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '设备编号',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color3333,
                              fontSize: 15,
                              fontWeight: FontWeight.bold),
                        ),
                      )),
                      Container(
                        width: 250,
                        alignment: Alignment.centerLeft,
                        child: Obx(() => TextField(
                              textDirection: TextDirection.ltr,
                              controller: TextEditingController(
                                  text: controller.params.value.code),
                              cursorColor: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color1c24),
                              style: StylesUtils.customTextStyle(
                                  fontSize: 16, color: ColorsUtils.color1c24),
                              textAlign: TextAlign.left,
                              textAlignVertical: TextAlignVertical.center,
                              cursorWidth: 2,
                              cursorRadius: const Radius.circular(2),
                              decoration: InputDecoration(
                                  hintText: '请输入',
                                  hintStyle: StylesUtils.customTextStyle(
                                      color: ColorsUtils.colorCccc,
                                      fontSize: 15),
                                  isDense: true,
                                  isCollapsed: true,
                                  fillColor: Colors.white,
                                  border: InputBorder.none,
                                  focusColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  contentPadding:
                                      const EdgeInsets.symmetric(vertical: 20),
                                  focusedBorder: InputBorder.none),
                              onChanged: (String text) {
                                controller.params.value.code = text;
                              },
                            )),
                      )
                    ],
                  )),
                ),
                SizedBox(
                  height: 50,
                  child: DrawUnderLine(
                      child: Row(
                    children: [
                      Expanded(
                          child: Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '设备名称',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color3333,
                              fontSize: 15,
                              fontWeight: FontWeight.bold),
                        ),
                      )),
                      Container(
                        width: 250,
                        alignment: Alignment.centerLeft,
                        child: Obx(() => TextField(
                              textDirection: TextDirection.ltr,
                              controller: TextEditingController(
                                  text: controller.params.value.name),
                              cursorColor: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color1c24),
                              style: StylesUtils.customTextStyle(
                                  fontSize: 16, color: ColorsUtils.color1c24),
                              textAlign: TextAlign.left,
                              textAlignVertical: TextAlignVertical.center,
                              cursorWidth: 2,
                              cursorRadius: const Radius.circular(2),
                              decoration: InputDecoration(
                                  hintText: '请输入',
                                  hintStyle: StylesUtils.customTextStyle(
                                      color: ColorsUtils.colorCccc,
                                      fontSize: 15),
                                  isDense: true,
                                  isCollapsed: true,
                                  fillColor: Colors.white,
                                  border: InputBorder.none,
                                  focusColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  contentPadding:
                                      const EdgeInsets.symmetric(vertical: 20),
                                  focusedBorder: InputBorder.none),
                              onChanged: (String text) {
                                controller.params.value.name = text;
                              },
                            )),
                      )
                    ],
                  )),
                ),
                SizedBox(
                  height: 50,
                  child: DrawUnderLine(
                      child: Row(
                    children: [
                      Expanded(
                          child: Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '存放位置',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color3333,
                              fontSize: 15,
                              fontWeight: FontWeight.bold),
                        ),
                      )),
                      Container(
                        width: 250,
                        alignment: Alignment.centerLeft,
                        child: Obx(() => TextField(
                              textDirection: TextDirection.ltr,
                              controller: TextEditingController(
                                  text: controller.params.value.location),
                              cursorColor: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color1c24),
                              style: StylesUtils.customTextStyle(
                                  fontSize: 16, color: ColorsUtils.color1c24),
                              textAlign: TextAlign.left,
                              textAlignVertical: TextAlignVertical.center,
                              cursorWidth: 2,
                              cursorRadius: const Radius.circular(2),
                              decoration: InputDecoration(
                                  hintText: '请输入',
                                  hintStyle: StylesUtils.customTextStyle(
                                      color: ColorsUtils.colorCccc,
                                      fontSize: 15),
                                  isDense: true,
                                  isCollapsed: true,
                                  fillColor: Colors.white,
                                  border: InputBorder.none,
                                  focusColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  contentPadding:
                                      const EdgeInsets.symmetric(vertical: 20),
                                  suffixIcon: Icon(
                                    AntdIcons.right,
                                    color: ColorsUtils.parseColorFromHexString(
                                        ColorsUtils.color9999),
                                    size: 15,
                                  ),
                                  focusedBorder: InputBorder.none),
                              onTap: () {},
                            )),
                      )
                    ],
                  )),
                ),
                SizedBox(
                  height: 50,
                  child: DrawUnderLine(
                      child: Row(
                    children: [
                      Expanded(
                          child: Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '设备类型',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color3333,
                              fontSize: 15,
                              fontWeight: FontWeight.bold),
                        ),
                      )),
                      Container(
                        width: 250,
                        alignment: Alignment.centerLeft,
                        child: Obx(() => TextField(
                              textDirection: TextDirection.ltr,
                              readOnly: true,
                              controller: controller.titleController.value,
                              cursorColor: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color1c24),
                              style: StylesUtils.customTextStyle(
                                  fontSize: 16, color: ColorsUtils.color1c24),
                              textAlign: TextAlign.left,
                              textAlignVertical: TextAlignVertical.center,
                              cursorWidth: 2,
                              cursorRadius: const Radius.circular(2),
                              decoration: InputDecoration(
                                  hintText: '请输入',
                                  hintStyle: StylesUtils.customTextStyle(
                                      color: ColorsUtils.colorCccc,
                                      fontSize: 15),
                                  isDense: true,
                                  isCollapsed: true,
                                  fillColor: Colors.white,
                                  border: InputBorder.none,
                                  focusColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  contentPadding:
                                      const EdgeInsets.symmetric(vertical: 20),
                                  suffixIcon: Icon(
                                    AntdIcons.right,
                                    color: ColorsUtils.parseColorFromHexString(
                                        ColorsUtils.color9999),
                                    size: 15,
                                  ),
                                  focusedBorder: InputBorder.none),
                              onTap: () {
                                showModalBottomSheet(
                                    context: context,
                                    backgroundColor: Colors.transparent,
                                    builder: (BuildContext context) {
                                      return BrnCommonActionSheet(
                                        actions: DeviceTypeEnum.values
                                            .map((e) => BrnCommonActionSheetItem(
                                                e.label,
                                                titleStyle:
                                                    StylesUtils.customTextStyle(
                                                        fontSize: 16,
                                                        color: e.label ==
                                                                controller
                                                                    .params
                                                                    .value
                                                                    .type
                                                                    ?.label
                                                            ? ColorsUtils
                                                                .color0052
                                                            : ColorsUtils
                                                                .color3333)))
                                            .toList(),
                                        cancelTitle: '取消',
                                        themeData: BrnActionSheetConfig(
                                            cancelStyle: BrnTextStyle(
                                              color: ColorsUtils
                                                  .parseColorFromHexString(
                                                      ColorsUtils.colorE34d),
                                              fontWeight: FontWeight.normal,
                                              fontSize: 14,
                                            ),
                                            topRadius: 10),
                                        clickCallBack: (int index,
                                            BrnCommonActionSheetItem
                                                actionItem) {
                                          controller.params.value.type =
                                              DeviceTypeEnum.values.firstWhere(
                                                  (element) =>
                                                      element.label ==
                                                      actionItem.title);
                                          controller.titleController.value
                                              .text = actionItem.title;
                                        },
                                      );
                                    });
                              },
                            )),
                      )
                    ],
                  )),
                ),
                SizedBox(
                  height: 50,
                  child: DrawUnderLine(
                      child: Row(
                    children: [
                      Expanded(
                          child: Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '所在部门',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color3333,
                              fontSize: 15,
                              fontWeight: FontWeight.bold),
                        ),
                      )),
                      Container(
                        width: 250,
                        alignment: Alignment.centerLeft,
                        child: Obx(() => TextField(
                              textDirection: TextDirection.ltr,
                              readOnly: true,
                              controller: controller.deptController.value,
                              cursorColor: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color1c24),
                              style: StylesUtils.customTextStyle(
                                  fontSize: 16, color: ColorsUtils.color1c24),
                              textAlign: TextAlign.left,
                              textAlignVertical: TextAlignVertical.center,
                              cursorWidth: 2,
                              cursorRadius: const Radius.circular(2),
                              decoration: InputDecoration(
                                  hintText: '请输入',
                                  hintStyle: StylesUtils.customTextStyle(
                                      color: ColorsUtils.colorCccc,
                                      fontSize: 15),
                                  isDense: true,
                                  isCollapsed: true,
                                  fillColor: Colors.white,
                                  border: InputBorder.none,
                                  focusColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  contentPadding:
                                      const EdgeInsets.symmetric(vertical: 20),
                                  suffixIcon: Icon(
                                    AntdIcons.right,
                                    color: ColorsUtils.parseColorFromHexString(
                                        ColorsUtils.color9999),
                                    size: 15,
                                  ),
                                  focusedBorder: InputBorder.none),
                              onTap: () {
                                ToolsUtils.openDataPicker(
                                    context,
                                    controller.depts
                                        .map((element) =>
                                            BrnMultiDataPickerEntity(
                                                text: element.name,
                                                value: element.deptId))
                                        .toList(), (List selectedIndexList) {
                                  int index = selectedIndexList[0];
                                  controller.deptController.value.text =
                                      controller.depts[index].name;
                                });
                              },
                            )),
                      )
                    ],
                  )),
                ),
                SizedBox(
                  height: 50,
                  child: DrawUnderLine(
                      child: Row(
                    children: [
                      Expanded(
                          child: Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '负责人',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color3333,
                              fontSize: 15,
                              fontWeight: FontWeight.bold),
                        ),
                      )),
                      Container(
                        width: 250,
                        alignment: Alignment.centerLeft,
                        child: Obx(() => TextField(
                              textDirection: TextDirection.ltr,
                              controller: TextEditingController(
                                  text: controller.params.value.charge),
                              cursorColor: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color1c24),
                              style: StylesUtils.customTextStyle(
                                  fontSize: 16, color: ColorsUtils.color1c24),
                              textAlign: TextAlign.left,
                              textAlignVertical: TextAlignVertical.center,
                              cursorWidth: 2,
                              cursorRadius: const Radius.circular(2),
                              decoration: InputDecoration(
                                  hintText: '请输入',
                                  hintStyle: StylesUtils.customTextStyle(
                                      color: ColorsUtils.colorCccc,
                                      fontSize: 15),
                                  isDense: true,
                                  isCollapsed: true,
                                  fillColor: Colors.white,
                                  border: InputBorder.none,
                                  suffixIcon: Icon(
                                    AntdIcons.right,
                                    color: ColorsUtils.parseColorFromHexString(
                                        ColorsUtils.color9999),
                                    size: 15,
                                  ),
                                  focusColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  contentPadding:
                                      const EdgeInsets.symmetric(vertical: 20),
                                  focusedBorder: InputBorder.none),
                              onChanged: (String text) {
                                controller.params.value.charge = text;
                              },
                            )),
                      )
                    ],
                  )),
                ),
                SizedBox(
                  height: 50,
                  child: DrawUnderLine(
                      child: Row(
                    children: [
                      Expanded(
                          child: Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '设备状态',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color3333,
                              fontSize: 15,
                              fontWeight: FontWeight.bold),
                        ),
                      )),
                      Container(
                        width: 250,
                        alignment: Alignment.centerLeft,
                        child: Obx(() => TextField(
                              textDirection: TextDirection.ltr,
                              readOnly: true,
                              controller: controller.statusController.value,
                              cursorColor: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color1c24),
                              style: StylesUtils.customTextStyle(
                                  fontSize: 16, color: ColorsUtils.color1c24),
                              textAlign: TextAlign.left,
                              textAlignVertical: TextAlignVertical.center,
                              cursorWidth: 2,
                              cursorRadius: const Radius.circular(2),
                              decoration: InputDecoration(
                                  hintText: '请输入',
                                  hintStyle: StylesUtils.customTextStyle(
                                      color: ColorsUtils.colorCccc,
                                      fontSize: 15),
                                  isDense: true,
                                  isCollapsed: true,
                                  fillColor: Colors.white,
                                  border: InputBorder.none,
                                  focusColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  contentPadding:
                                      const EdgeInsets.symmetric(vertical: 20),
                                  suffixIcon: Icon(
                                    AntdIcons.right,
                                    color: ColorsUtils.parseColorFromHexString(
                                        ColorsUtils.color9999),
                                    size: 15,
                                  ),
                                  focusedBorder: InputBorder.none),
                              onTap: () {
                                showModalBottomSheet(
                                    context: context,
                                    backgroundColor: Colors.transparent,
                                    builder: (BuildContext context) {
                                      return BrnCommonActionSheet(
                                        actions: DeviceStatusEnum.values
                                            .map((e) => BrnCommonActionSheetItem(
                                                e.label,
                                                titleStyle:
                                                    StylesUtils.customTextStyle(
                                                        fontSize: 16,
                                                        color: e.label ==
                                                                controller
                                                                    .params
                                                                    .value
                                                                    .status
                                                                    ?.label
                                                            ? ColorsUtils
                                                                .color0052
                                                            : ColorsUtils
                                                                .color3333)))
                                            .toList(),
                                        cancelTitle: '取消',
                                        themeData: BrnActionSheetConfig(
                                            cancelStyle: BrnTextStyle(
                                              color: ColorsUtils
                                                  .parseColorFromHexString(
                                                      ColorsUtils.colorE34d),
                                              fontWeight: FontWeight.normal,
                                              fontSize: 14,
                                            ),
                                            topRadius: 10),
                                        clickCallBack: (int index,
                                            BrnCommonActionSheetItem
                                                actionItem) {
                                          controller.params.value.status =
                                              DeviceStatusEnum.values
                                                  .firstWhere((element) =>
                                                      element.label ==
                                                      actionItem.title);
                                          controller.statusController.value
                                              .text = actionItem.title;
                                        },
                                      );
                                    });
                              },
                            )),
                      )
                    ],
                  )),
                ),
              ],
            ),
          ),
          extendBody: true,
          bottomSheet: Container(
            height: 50,
            decoration: BoxDecoration(
                color: Colors.white,
                border: Border(
                    top: BorderSide(
                        color: ColorsUtils.parseColorFromHexString(
                            ColorsUtils.colorE4e4)))),
            child: Row(
              children: [
                Expanded(
                    child: Center(
                  child: TextButton(
                      onPressed: () {},
                      child: Text(
                        '重置',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color0052, fontSize: 16),
                      )),
                )),
                Expanded(
                    child: Container(
                  alignment: Alignment.center,
                  color: ColorsUtils.parseColorFromHexString(
                      ColorsUtils.color0052),
                  child: TextButton(
                    onPressed: () {},
                    style: ButtonStyle(
                      padding: MaterialStateProperty.all(EdgeInsets.zero),
                    ),
                    child: Text(
                      '确定',
                      style: StylesUtils.customTextStyle(fontSize: 16),
                    ),
                  ),
                ))
              ],
            ),
          ),
        );
      },
      init: controller,
    );
  }
}
